﻿Imports System.Collections.Specialized
Imports System.IO
Imports System.Text
Imports Paypal_Manager.Utility
Imports Paypal_Manager.Utility.Http
Namespace Service
    Public Class Paypal
        Implements IDisposable
        Private Http As Http = Nothing

#Region "Events"
        Public Event Notify(ByVal Text As String)
#End Region

#Region "Properties"
        Private _Account As String()
        Public Property Account As String()
            Get
                Return _Account
            End Get
            Set(ByVal value As String())
                _Account = value
            End Set
        End Property

        Private _Online As Boolean = False
        Public Property Online As Boolean
            Get
                Return _Online
            End Get
            Set(ByVal value As Boolean)
                _Online = value
            End Set
        End Property

        'Will implement later for non US paypals.
        Private _Lang As String = String.Empty
        Public Property Lang As String
            Get
                Return _Lang
            End Get
            Set(ByVal value As String)
                _Lang = value
            End Set
        End Property
#End Region

#Region "Constructor"
        Public Sub New(ByVal info As String())
            Account = info
        End Sub
#End Region

#Region "Deconstructor"
        Private disposedValue As Boolean
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    If Not IsNothing(Http) Then Http.Dispose()
                End If
            End If
            Me.disposedValue = True
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

#Region "Methods"
        Private Sub NewSession()
            If Not IsNothing(Http) Then Http.Dispose()
            Http = New Utility.Http
            Online = False
        End Sub
#End Region

#Region "API"
        Public Sub Login()
            NewSession()
            With Http
                RaiseEvent Notify(String.Format("Attempting to log into account '{0}'.", Account(0)))

                .DebugMode = True
                .AutoRedirect = True

                Dim hr As HttpResponse = .GetResponse(Verb._GET, "https://www.paypal.com/us/home")

                If Not hr.Html.Contains("Log in") Then
                    RaiseEvent Notify("Could not login: missing element.")
                End If
                Dim csrf As String = .ParseBetween(hr.Html, "csrfModel.returnedCsrf"" value=""", """ />", "csrfModel.returnedCsrf"" value=""".Length)

                Dim PostData As New StringBuilder
                PostData.Append("csrfModel.returnedCsrf=" & csrf)
                PostData.Append("&login_email=" & .UrlEncode(Account(0).ToLower))
                PostData.Append("&login_password=" & .UrlEncode(Account(1).ToLower))
                PostData.Append("&submit.x=Log+in")

                .ContentType = "application/x-www-form-urlencoded"
                .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                .Referer = "https://www.paypal.com/home"

                Dim headers As New NameValueCollection
                headers.Add("Content-Length", PostData.ToString.Length)

                hr = .GetResponse(Verb._POST, "https://www.paypal.com/us/cgi-bin/webscr?cmd=_login-submit", PostData.ToString, headers)


                hr = .GetResponse(Verb._GET, "https://www.paypal.com/us/home")

                If hr.Html.Contains("balance") Then
                    Online = True
                    RaiseEvent Notify("Logged in")
                Else
                    Online = False
                    RaiseEvent Notify("Could not log in: please check username or password.")
                End If
            End With
        End Sub
        Public Sub Load()
            With Http
                .DebugMode = True
                .AutoRedirect = True

                'Will be done in next version.
            End With
        End Sub
#End Region

    End Class
End Namespace